﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ECommerce.Model.Advertisement;
using Jzy.Core.Common;
using ECommerce.DAL.Advertisement;
using System.IO;
using Jzy.Core.Attach;

namespace ECommerce.BLL.Advertisement
{
    public class AdvertisementBO : CommonOperate<AdvertisementInfo>
    {
        private AdvertisementDAL _advertisement;
        public AdvertisementBO()
        {
            _advertisement = new AdvertisementDAL();
        }

        public AdvertisementInfo GetInfoByName(string name)
        {
            return _advertisement.GetInfoByName(name);
        }

        public void DeleteImage(int id)
        {
            AdvertisementInfo info = this.GetInfo(id);
            string phyPath = System.Web.HttpContext.Current.Server.MapPath(info.SmallImageAbsPath);
            if (File.Exists(phyPath))
            {
                File.Delete(phyPath);
            }

            phyPath = System.Web.HttpContext.Current.Server.MapPath(info.BigImageAbsPath);
            if (File.Exists(phyPath))
            {
                File.Delete(phyPath);
            }
        }

        public void SaveImage(AdvertisementInfo info, System.Web.HttpPostedFile file)
        {
            UserImage userImage = new UserImage("Advertisement");
            info.BigImage = userImage.Save(file);
            info.SmallImage = userImage.Save(file, 80);
        }
    }
}
